package org.example.controller;

import io.swagger.annotations.*;
import org.example.dto.LoginDTO;
import org.example.dto.PassDTO;
import org.example.dto.RegisterDTO;
import org.example.po.ShoppingUser;
import org.example.rvo.ResultVO;
import org.example.service.ShoppingUserService;
import org.example.util.JWTUtil;
import org.example.util.TUserThreadLocal;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/user")
@Api(tags = "账户验证接口", description = "包括用户登录、注册、查询修改等操作。")
public class ShoppingUserController {

    @Resource
    private ShoppingUserService shoppingUserService;
    @ApiResponses({
            @ApiResponse(code = 1000001, message = "登录成功"),
            @ApiResponse(code = 2000001, message = "登录失败")   //不同返回状态码描述
    })
    @ApiOperation("用户登录操作")   //接口描述
    @PostMapping(value = "/login",produces = "application/json") //api/user/login
    public ResultVO login(@ApiParam("post请求的对象") @RequestBody LoginDTO loginDTO) throws Exception {
        return shoppingUserService.login(loginDTO.getUserName(), loginDTO.getUserPass());
    }

    //post
    @PostMapping(value="/register")
    public ResultVO register(RegisterDTO registerDTO) throws Exception {
        return shoppingUserService.register(registerDTO);
    }

    //get
    @GetMapping(value = "/queryUser",produces = "application/json")
    public ResultVO queryUser(HttpServletRequest request) throws Exception{


        // ShoppingUser shoppingUser =(ShoppingUser) request.getAttribute("shoppingUser");
        ShoppingUser shoppingUser =(ShoppingUser) TUserThreadLocal.get();
        return shoppingUserService.queryUser(shoppingUser);
    }

    //update
    @PutMapping("/updateUser")
    public ResultVO updateUser(@RequestBody PassDTO passDTO, HttpServletRequest request) throws Exception{
      //  ShoppingUser shoppingUser =(ShoppingUser) request.getAttribute("shoppingUser");
        ShoppingUser shoppingUser =(ShoppingUser) TUserThreadLocal.get();

        System.out.println(request.getRemoteAddr()+":"+request.getServerPort());
        shoppingUser.setUserPass(passDTO.getPass());
        return shoppingUserService.updateUser(shoppingUser);
    }

    //delete
    //@DeleteMapping


}